/*============================================================================*/
/*                        SV C CE SOFTWARE GROUP                              */
/*============================================================================*/
/*                        OBJECT SPECIFICATION                                */
/*============================================================================*
* C Source:         main.c
* Instance:         ?????
* %version:         1 %
* %created_by:      Emmanuel Dominguez  Roberto Gonzalez  %
* %date_created:    Tue Nov  13 ??:??:?? 2012 %
*=============================================================================*/
/* DESCRIPTION : Rotabit para PIC18F4550                                      */
/*============================================================================*/
/* FUNCTION COMMENT : This file describes the C source template according to  */
/* the new software platform                                                  */
/*                                                                            */
/*============================================================================*/
/*                               OBJECT HISTORY                               */
/*============================================================================*/
/*  REVISION |   DATE      |                               |      AUTHOR      */
/*----------------------------------------------------------------------------*/
/*  1.0      | 13/10/2012  |                              |208435812/302226383*/
/* Integration under Continuus CM  Emmanuel Dominguez  Roberto Gonzalez       */
/*============================================================================*/
/* Includes */
/* -------- */
#include<stdio.h> 
#include<typedef.h>

/* Functions macros, constants, types and datas         */
/* ---------------------------------------------------- */
/* Functions macros */
void timer0_initCfg(void);
void initcpu(void);
void timer0_Interrupt(void);
void inithw(void);
void interrupt_initCfg(void);
void Enable_Interrupts(void);
void Disable_Interrupts();
void Left(void);
void Right(void);
void blink(void);
void turnOff(void);
void (*apuntador)(void);

#pragma interrupt timer0_Interrupt

#pragma	config WDT = OFF
#pragma config FOSC = INTOSCIO_EC
#pragma	config MCLRE = ON
//#pragma config DEBUG = ON

#pragma code high_vector=0x08
void interrupt_at_high_vector(void)
{
_asm GOTO timer0_Interrupt _endasm
}
#pragma code /* return to the default code section */

unsigned int ruw_TimerCounter=0x00;

/*==================================================*/ 
/* Definition of constants                          */
/*==================================================*/ 
/* BYTE constants */
/* WORD constants */


/* LONG and STRUCTURE constants */

/*======================================================*/ 
/* Definition of RAM variables                          */
/*======================================================*/ 
/* BYTE RAM variables */
/* WORD RAM variables */

/* LONG and STRUCTURE RAM variables */

/*======================================================*/ 
/* close variable declaration sections                  */
/*======================================================*/ 

/* Private defines */

/* Private functions prototypes */
/* ---------------------------- */

/* Exported functions prototypes */
/* ----------------------------- */


/* ????? functions */
/* ---------------- */
/**************************************************************
 *  Name                 :	main
 *  Description          :	funcion principal del programa
 *  Parameters           :  none
 *  Return               :	none
 *  Critical/explanation :	none
 **************************************************************/



void main (void)
{

	initcpu();
	inithw();
	timer0_initCfg();
	interrupt_initCfg();
	while(1)
	{
	}
}

void timer0_initCfg()
{
    T0CONbits.TMR0ON = 0;
    T0CONbits.T08BIT = 0;
    T0CONbits.T0CS = 0;
    T0CONbits.T0SE = 0;
    T0CONbits.PSA = 1;
    T0CONbits.T0PS2 = 0;
    T0CONbits.T0PS1 = 0;
    T0CONbits.T0PS0 = 0;
    TMR0L = 0x2F;
    TMR0H = 0xF8;
    INTCONbits.TMR0IF = 0;
    T0CONbits.TMR0ON = 1;
}
void initcpu()
{

	//#FUSES MCLR                   //Master Clear pin used
    OSCCONbits.SCS0 = 0;
    OSCCONbits.SCS1 = 1; 

/* bit 1-0 SCS1:SCS0: System Clock Select bits   */
/* 1x = Internal oscillator block                */
/* 01 = Timer1 oscillator                        */
/* 00 = Primary oscillator                       */

    OSCCONbits.IRCF0 = 1;
    OSCCONbits.IRCF1 = 1;
    OSCCONbits.IRCF2 = 1;

/* bit 6-4 IRCF2:IRCF0: Internal Oscillator Frequency Select bits */
/* 111 = 8 MHz (INTOSC drives clock directly)                     */
/* 110 = 4 MHz                                                    */
/* 101 = 2 MHz                                                    */
/* 100 = 1 MHz(3)                                                 */
/* 011 = 500 kHz                                                  */
/* 010 = 250 kHz                                                  */
/* 001 = 125 kHz                                                  */
/* 000 = 31 kHz (from either INTOSC/256 or INTRC directly)(2)     */

    OSCCONbits.IDLEN = 0;

/* bit 7 IDLEN: Idle Enable bit                          */
/* 1 = Device enters Idle mode on SLEEP instruction      */
/* 0 = Device enters Sleep mode on SLEEP instruction     */

    RCONbits.IPEN = 1;
/*  IPEN: Interrupt Priority Enable bit
    1 = Enable priority levels on interrupts
    0 = Disable priority levels on interrupts (PIC16CXXX Compatibility mode)
*/
}
void timer0_Interrupt()
{
//printf("%i \n",ruw_TimerCounter); 

	switch(ruw_TimerCounter){
		case 0xFFFF:
			ruw_TimerCounter = 1;
		break;
		case 200:
			turnOff();
			ruw_TimerCounter++;
		break;
		case 1000:
			blink();
			ruw_TimerCounter=0;
		break;
		default:
			ruw_TimerCounter++;
		break;
	}
	timer0_initCfg();
}

void inithw(void)
{
	PORTD = 0;
    LATD = 0;
    TRISD = 0;
	PORTB=0;
		    LATB = 0;
		    TRISB = 0;
}
void interrupt_initCfg()
{
    INTCON3bits.INT1IF = 0;
/*  INT1IF: INT1 External Interrupt Flag bit
    1 = The INT1 external interrupt occurred (must be cleared in software)
    0 = The INT1 external interrupt did not occur
*/

    INTCON3bits.INT2IF = 0;
/*  INT2IF: INT2 External Interrupt Flag bit
    1 = The INT2 external interrupt occurred (must be cleared in software)
    0 = The INT2 external interrupt did not occur
*/

    INTCON3bits.INT1IE = 0;
/*  INT1IE: INT1 External Interrupt Enable bit
    1 = Enables the INT1 external interrupt
    0 = Disables the INT1 external interrupt
*/

    INTCON3bits.INT2IE = 0;
/*  INT2IE: INT2 External Interrupt Enable bit
    1 = Enables the INT2 external interrupt
    0 = Disables the INT2 external interrupt
*/

    INTCON3bits.INT1IP = 0;
/*  INT1IP: INT1 External Interrupt Priority bit
    1 = High priority
    0 = Low priority
*/
    INTCON3bits.INT2IP = 0;
/*  INT2IP: INT2 External Interrupt Priority bit
    1 = High priority
    0 = Low priority
*/

    INTCON2bits.RBIP = 0;
/*  RBIP: RB Port Change Interrupt Priority bit
    1 = High priority
    0 = Low priority
*/

    INTCON2bits.TMR0IP = 1;
/*  TMR0IP: TMR0 Overflow Interrupt Priority bit
    1 = High priority
    0 = Low priority
*/

    INTCON2bits.INTEDG2 = 0;
/*  INTEDG2: External Interrupt 2 Edge Select bit
    1 = Interrupt on rising edge
    0 = Interrupt on falling edge
*/

    INTCON2bits.INTEDG1 = 0;
/*  INTEDG1: External Interrupt 1 Edge Select bit
    1 = Interrupt on rising edge
    0 = Interrupt on falling edge
*/

    INTCON2bits.INTEDG0 = 0;
/*  INTEDG0: External Interrupt 0 Edge Select bit
    1 = Interrupt on rising edge
    0 = Interrupt on falling edge
*/

    INTCON2bits.NOT_RBPU = 0;
/*  RBPU: PORTB Pull-up Enable bit
    1 = All PORTB pull-ups are disabled
    0 = PORTB pull-ups are enabled by individual port latch values
*/

    INTCONbits.RBIF = 0;
/*  RBIF: RB Port Change Interrupt Flag bit
    1 = At least one of the RB7:RB4 pins changed state (must be cleared in software)
    0 = None of the RB7:RB4 pins have changed state
*/

    INTCONbits.INT0IF = 0;
/*  INT0IF: INT0 External Interrupt Flag bit
    1 = The INT0 external interrupt occurred (must be cleared in software)
    0 = The INT0 external interrupt did not occur
*/

    INTCONbits.TMR0IF = 0;
/*  TMR0IF: TMR0 Overflow Interrupt Flag bit
    1 = TMR0 register has overflowed (must be cleared in software)
    0 = TMR0 register did not overflow
*/

    INTCONbits.RBIE = 0;
/*  RBIE: RB Port Change Interrupt Enable bit
    1 = Enables the RB port change interrupt
    0 = Disables the RB port change interrupt
*/

    INTCONbits.INT0IE = 0;
/*  INT0IE: INT0 External Interrupt Enable bit
    1 = Enables the INT0 external interrupt
    0 = Disables the INT0 external interrupt
*/

    INTCONbits.TMR0IE = 1;
/*  TMR0IE: TMR0 Overflow Interrupt Enable bit
    1 = Enables the TMR0 overflow interrupt
    0 = Disables the TMR0 overflow interrupt
*/

    INTCONbits.PEIE = 0;
/*  PEIE/GIEL: Peripheral Interrupt Enable bit
    When IPEN = 0:
    1 = Enables all unmasked peripheral interrupts
    0 = Disables all peripheral interrupts
    When IPEN = 1:
    1 = Enables all low priority peripheral interrupts
    0 = Disables all low priority peripheral interrupts
*/
    Enable_Interrupts();
}
void Enable_Interrupts()
{
    INTCONbits.GIE = 1;
/*	GIE/GIEH: Global Interrupt Enable bit
    When IPEN = 0:
    1 = Enables all unmasked interrupts
    0 = Disables all interrupts
    When IPEN = 1:
    1 = Enables all high priority interrupts
    0 = Disables all high priority interrupts
*/
}

void Disable_Interrupts()
{
    INTCONbits.GIE = 0;
/*	GIE/GIEH: Global Interrupt Enable bit
    When IPEN = 0:
    1 = Enables all unmasked interrupts
    0 = Disables all interrupts
    When IPEN = 1:
    1 = Enables all high priority interrupts
    0 = Disables all high priority interrupts
*/
}

/* ????? functions */
/* ---------------- */
/**************************************************************
 *  Name                 :	blink
 *  Description          :	se encarga de cambiar los bits que encienden los leds
 *  Parameters           :  none
 *  Return               :	none
 *  Critical/explanation :  none
 **************************************************************/

void blink(){
	if(apuntador==Left){
		apuntador=Right;
	}else{
		apuntador=Left;
	}
	(*apuntador)();
}
/**************************************************************
 *  Name                 :	Right
 *  Description          :	Prende el bit 8
 *  Parameters           :  none
 *  Return               :	none
 *  Critical/explanation :  none
 **************************************************************/

void Right(){
	PORTD=(1<<0);
}
/**************************************************************
 *  Name                 :	Left
 *  Description          :	prende el bit 0
 *  Parameters           :  none
 *  Return               :	none
 *  Critical/explanation :  none
 **************************************************************/

void Left(){
	PORTD=(1<<7);
}

/**************************************************************
 *  Name                 :	turnOff
 *  Description          :	apaga todos los leds
 *  Parameters           :  none
 *  Return               :	none
 *  Critical/explanation :  none
 **************************************************************/

void turnOff(){
	PORTD=0;
}